/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmReportes.java
 *
 * Created on 02-jun-2011, 14:44:13
 */
package verins.GUI;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import verins.BO.ReportesBO;
import verins.Utils.Comunes;
import verins.Utils.ImprimirReportes;
import verins.beans.Partidopolitico;
import verins.beans.Partidopoliticoxprocesoelectoral;
import verins.beans.Procesoelectoral;
import verins.beans.Reporte;

/**
 *
 * @author hectdx
 */
public class frmReportes extends javax.swing.JFrame {
List lstRepDatos=null;
List lstRepProces=null;
List lstRepSegur=null;
List lstReportes=null;
List lstReportesTabla=null;
public Procesoelectoral procesoEle=null;
List lstParams=new ArrayList();
List lstValores=new ArrayList();

    /** Creates new form frmReportes */
    public frmReportes(Procesoelectoral proceso) {
        procesoEle=proceso;
        initComponents();
        verins.Utils.Comunes.centrarVentana(this);
        iniciarReportesDisponibles();
        getRootPane().setDefaultButton(btnExportar);
    }
    
    private void llenarTabla(){
        verins.Utils.Comunes.limpiarTabla(tblReportes);
        DefaultTableModel dtm=(DefaultTableModel)tblReportes.getModel();
        
        if (cmbTipoReporte.getSelectedIndex()>=0){
            
            if (cmbTipoReporte.getSelectedIndex()==1){
                lstReportesTabla=lstRepDatos;
            }
            /*if (cmbTipoReporte.getSelectedIndex()==2){
                lstReportesTabla=lstRepProces;
            }*/
            if (cmbTipoReporte.getSelectedIndex()==2){
                lstReportesTabla=lstRepSegur;
            }
            
            if (lstReportesTabla!=null && lstReportesTabla.size()>0){
                Iterator it=lstReportesTabla.iterator();
                while(it.hasNext()){
                    Object fila[]=new Object[] {(Reporte)it.next()};
                    dtm.addRow(fila);
                }
            }
        }
    
    }
    
    private void iniciarReportesDisponibles(){
        lstRepDatos=new ArrayList();
        lstRepProces=new ArrayList();
        lstRepSegur=new ArrayList();
        
        lstReportes=ReportesBO.findAll();
        Iterator it=lstReportes.iterator();
        Reporte rep=null;
        while(it.hasNext()){
           rep=(Reporte)it.next();
           switch (rep.getTipo()){
               case 1:  lstRepDatos.add(rep);
                        break;
               case 2:  lstRepProces.add(rep);
                        break;
               case 3:  lstRepSegur.add(rep);
                        break;
               default: break;
           }
            
        }   
    }
    
    private  boolean  validarCampos(){
    boolean valido=true;
    
    if (procesoEle==null){
        verins.Utils.Mensajes.alerta("Debe elegir un proceso electoral");
        return false;
    }
    return valido;
    }
    
    
   public void setProcesoElec(Procesoelectoral proceso){
       procesoEle=proceso;
       txtProcesoElec.setText(procesoEle.getNombre());
   }
   
   public void bloquearComboPartido(){
       if (lstReportesTabla!=null && lstReportesTabla.size()>0){
           int fila=tblReportes.getSelectedRow();
                if (fila>=0){
                    String desc=lstReportesTabla.get(fila).toString();
                    if (desc.contains("artido")&& !desc.contains("partidos"))
                        cmbPartido.setEnabled(true);
                    else
                        cmbPartido.setEnabled(false);
                }
       }
   }
   
   private void obtenerParametros(){
   
       lstParams.add("pProceso");
       lstValores.add(procesoEle.getIdprocesoelectoral());
       lstParams.add("pNombreProceso");
       lstValores.add(procesoEle.getNombre().toUpperCase());
       if (cmbPartido.getItemCount()>0){
           lstParams.add("pNombrePartido");
           lstValores.add(cmbPartido.getSelectedItem().toString().toUpperCase());
           lstParams.add("pPartido");
           lstValores.add(((Partidopoliticoxprocesoelectoral)cmbPartido.getSelectedItem()).getPartidopoliticoxprocesoelectoralPK().getIdpartidopolitico());
       }
       
   }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbTipoReporte = new javax.swing.JComboBox();
        jpPlant01 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtProcesoElec = new javax.swing.JTextField();
        btnBusq = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cmbPartido = new javax.swing.JComboBox();
        btnExportar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReportes = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reportes");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros del reporte"));

        jLabel1.setText("Tipo reporte");

        cmbTipoReporte.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleccione>", "Procesamiento", "Seguridad" }));
        cmbTipoReporte.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbTipoReporteItemStateChanged(evt);
            }
        });

        jLabel2.setText("Proceso electoral");

        txtProcesoElec.setEnabled(false);

        btnBusq.setIcon(new javax.swing.ImageIcon(getClass().getResource("/verins/GUI/lupa.png"))); // NOI18N
        btnBusq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusqActionPerformed(evt);
            }
        });

        jLabel3.setText("Partido político");

        cmbPartido.setEnabled(false);

        javax.swing.GroupLayout jpPlant01Layout = new javax.swing.GroupLayout(jpPlant01);
        jpPlant01.setLayout(jpPlant01Layout);
        jpPlant01Layout.setHorizontalGroup(
            jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPlant01Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpPlant01Layout.createSequentialGroup()
                        .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBusq, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jpPlant01Layout.setVerticalGroup(
            jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPlant01Layout.createSequentialGroup()
                .addGroup(jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpPlant01Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnBusq))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel1)
                        .addGap(48, 48, 48)
                        .addComponent(cmbTipoReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jpPlant01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbTipoReporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jpPlant01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnExportar.setText("Exportar");
        btnExportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        tblReportes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre reporte"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblReportes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblReportesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblReportes);
        tblReportes.getColumnModel().getColumn(0).setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(345, 345, 345)
                        .addComponent(btnExportar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 257, Short.MAX_VALUE)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 761, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnExportar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void bloquearBusqueda(){
        btnBusq.setVisible(false);
        txtProcesoElec.setEnabled(false);
    }
    
    private void cmbTipoReporteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTipoReporteItemStateChanged
        // TODO add your handling code here:
        llenarTabla();
    }//GEN-LAST:event_cmbTipoReporteItemStateChanged

    private void btnExportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportarActionPerformed
        // TODO add your handling code here:
        String nombJasper="";
        Reporte rep=null;
        if (validarCampos()){
            if (lstReportesTabla!=null){
                if (cmbTipoReporte.getSelectedIndex()!=0){
                    int fila=tblReportes.getSelectedRow();
                    if (fila>=0){
                       rep=(Reporte)lstReportesTabla.get(fila);
                       nombJasper=rep.getArchivo();
                    }
                }
            }

            if(!nombJasper.equals("")){
                try {
                    obtenerParametros();
                    System.out.println("ini report..");

                    new ImprimirReportes().ejecutarReporte(nombJasper, lstParams, lstValores);
                } catch (ClassNotFoundException ex) {

                }
            }
        }
    }//GEN-LAST:event_btnExportarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBusqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusqActionPerformed
        // TODO add your handling code here:
        frmBusquedaProcesoElectoral frm=new frmBusquedaProcesoElectoral(null,this);
        frm.setearBotonesBusqueda();
        frm.setVisible(true);
        procesoEle=frm.proceso;
        if (procesoEle!=null){
            procesoEle=frm.proceso;
            setProcesoElec(procesoEle);
            actualizarComboPartidos();
        }
    }//GEN-LAST:event_btnBusqActionPerformed

    private void tblReportesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblReportesMouseClicked
        // TODO add your handling code here:
        bloquearComboPartido();
    }//GEN-LAST:event_tblReportesMouseClicked

    private void  actualizarComboPartidos(){
        cmbPartido.removeAllItems();
        List lst=procesoEle.getPartidopoliticoxprocesoelectoralList();
        Comunes.llenarCombo(cmbPartido, lst);
        cmbPartido.setEnabled(cmbPartido.getItemCount()>0);
    }
    
   
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmReportes(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBusq;
    private javax.swing.JButton btnExportar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbPartido;
    private javax.swing.JComboBox cmbTipoReporte;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jpPlant01;
    private javax.swing.JTable tblReportes;
    private javax.swing.JTextField txtProcesoElec;
    // End of variables declaration//GEN-END:variables
}
